/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SESSIONMANAGER_H
#define SESSIONMANAGER_H

#include <deque>

#include "Connection.h"

namespace Chronos {

class Database;
class EventServer;
class PlayerSession;
class LoginSession;
class PlayerCreationSession;
class Parser;
class StringBuilder;

class SessionVisitor {
public:
    virtual ~SessionVisitor() {}
    virtual void handle(PlayerSession& session) = 0;
};


class SessionManager {
public:
    SessionManager(int port, EventServer& eventServer_, Parser* parser_, Database& database_);
    ~SessionManager();
    void handlePulse();
    void addPlayer(const std::string& name, Connection* connection);

    PlayerSession* findPlayerSession(const std::string& name) const;
    void visitSessions(SessionVisitor& visitor);

    Parser& getParser() const { return *parser; }
    void printStatus(StringBuilder& buffer) const;
private:

    void handlePlayerSessions();
    void handleLoginSessions();
    void handlePlayerCreationSessions();
    void checkNewConnections();

    EventServer& eventServer;
    SocketServer socketServer;
    Parser* parser;
    Database& database;

    typedef std::deque<PlayerSession*> PlayerSessions;
    typedef PlayerSessions::iterator PlayerSessionsIter;
    typedef PlayerSessions::const_iterator PlayerSessionsConstIter;
    PlayerSessions players;

    typedef std::deque<LoginSession*> LoginSessions;
    typedef LoginSessions::iterator LoginSessionsIter;
    LoginSessions logins;

    typedef std::deque<PlayerCreationSession*> CreationSessions;
    typedef CreationSessions::iterator CreationSessionsIter;
    CreationSessions creationSessions;

    SessionManager(const SessionManager&);
    SessionManager& operator=(const SessionManager&);
};

}

#endif

